package dao.impl;

import dao.UserDao;
import entity.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    public boolean insertUser(User user) {
        try{
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\User.txt",true));
            bufferedWriter.write(user.getId()+","+user.getUserName()+","+user.getPassWord()+","+user.getQuestion()+","+user.getAnswer()+","+user.getLevel());
            System.out.println(user.getUserName());
            bufferedWriter.newLine();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("io异常，添加失败！");
            return false;
        }
    }

    @Override
    public boolean deleteUserById(Integer id) {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string ;
            while ((string=bufferedReader.readLine())!=null){
                String[]dates= string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i++) {
                User user=userList.get(i);
                if ( (user.getId()).equals(id) ){
                    userList.remove(i);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\User.txt"));
            for (int i = 0; i < userList.size(); i++) {
                User user=userList.get(i);
                String returnString =String.format("%s,%s,%s,%s,%s,%s",user.getId(),user.getUserName(),user.getPassWord(),user.getQuestion(),user.getAnswer(),user.getLevel());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedReader.close();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("IO异常，删除失败！");
            return false;
        }
    }

    @Override
    public boolean updateUserById(Integer id,User user) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                User user1=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user1);
            }
            for (int i = 0; i < userList.size(); i++) {
                User user1= userList.get(i);
                if (user1.getId().equals(id)){
                    userList.set(i,user);
                }
            }
            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("SecondaryTradingSystem\\User.txt"));
            for (int i = 0; i < userList.size(); i++) {
                User user1=userList.get(i);
                String returnString=String.format("%s,%s,%s,%s,%s,%s",user1.getId(),user1.getUserName(),user1.getPassWord(),user1.getQuestion(),user1.getAnswer(),user1.getLevel());
                bufferedWriter.write(returnString);
                bufferedWriter.newLine();
            }
            bufferedReader.close();
            bufferedWriter.close();
            return true;
        }catch (IOException e){
            System.out.println("io异常，更改用户信息失败！");
            return false;
        }
    }

    @Override
    public User selectUserById(Integer id) {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String [] dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i++) {
                User user=userList.get(i);
                if (user.getId().equals(id)){
                    return user;
                }
            }
            bufferedReader.close();
        }catch (IOException e){
            System.out.println("文件未创建！");
            return null;
        }
        return null;
    }

    @Override
    public User selectUserByUserName(String userName) {
        try{
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[] dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            for (int i = 0; i < userList.size(); i++) {
                User user=userList.get(i);
                if (user.getUserName().equals(userName)){
                    return user;
                }
            }
            bufferedReader.close();
        }catch (IOException e) {
            System.out.println("io异常，查询用户信息失败！");
        }
        return null;
    }

    @Override
    public List<User> selectAllUser() {
        try {
            BufferedReader bufferedReader=new BufferedReader(new FileReader("SecondaryTradingSystem\\User.txt"));
            List<User> userList=new ArrayList<>();
            String string;
            while ((string=bufferedReader.readLine())!=null){
                String[]dates=string.split(",");
                User user=new User(Integer.parseInt(dates[0]),dates[1],dates[2],dates[3],dates[4],Integer.parseInt(dates[5]));
                userList.add(user);
            }
            bufferedReader.close();
            return userList;
        }catch (IOException e){
            System.out.println("io异常，查询用户信息失败！");
        }
        return null;
    }
}



